package com.rongke.alipayUtil.alipay.util;


import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.rongke.alipayUtil.config.AlipayConfig;

/**
 * Created by bilibili on 2017/9/15.
 */

public class AlipayRefundUtil {

    /**  支付宝退款接口
     * @param out_trade_no 订单支付时传入的商户订单号,不能和支付宝交易号（trade_no）同时为空。
     * @param trade_no 支付宝交易号
     * @param refund_amount 需要退款的金额，该金额不能大于订单金额,单位为元，支持两位小数
     * @return 将提示信息返回
     */
    public  synchronized static  String alipayRefundRequest(String out_trade_no,String trade_no,double refund_amount) {
        String returnStr = null;
        String out_request_no="HZ01RF001";
        try {
            AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.request_url, AlipayConfig.app_id, AlipayConfig.private_refund_key, AlipayConfig.format, AlipayConfig.input_charset, AlipayConfig.ali_refund_key, "RSA2");
            AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
            request.setBizContent("{" +
                    "\"out_trade_no\":\"" + out_trade_no + "\"," +
                    "\"trade_no\":\"" + trade_no + "\"," +
                    "\"refund_amount\":\"" + refund_amount + "\"," +
                    "\"refund_reason\":\"正常退款\"," +
                    "\"out_request_no\":\"" + out_request_no + "\"" +
                    " }");
            AlipayTradeRefundResponse response;
            response = alipayClient.execute(request);
            if (response.isSuccess()) {
                System.out.println("支付宝退款成功");
            } else {
                returnStr = response.getSubMsg();//失败会返回错误信息
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnStr;
    }

    public static void main(String str[]){
      String strq=  alipayRefundRequest("797a5c10cdf4902748beadb00bbbc202","",0.09D);
        System.out.println(strq);
    }

}
